<template>
	<view style="padding: 40rpx 20rpx">
		<scan></scan>
		<tn-button style="margin-right: 20rpx;margin-bottom: 20rpx;" @tap="getBluetooth()" height="60rpx"
			backgroundColor="tn-bg-blue" fontColor="tn-color-white">蓝牙打印机:{{bluetoothStyle}}</tn-button>
		<tn-button v-if="bluetoothStyle != '未连接'" style="margin-bottom: 20rpx;" @tap="closeBlue()" height="60rpx"
			backgroundColor="tn-bg-red" fontColor="tn-color-white">断开连接</tn-button>
		<view class="search-view">
			<view class="search">
				<text @tap="scanCode()" style="font-size: 50rpx;margin-right: 20rpx;" class="tn-icon-scan"></text>
				<input v-model="waybill_no" @confirm="searchNum()" style="font-size: 28rpx;width: 100%;"
					class="uni-input" confirm-type="search" placeholder="请输入或扫描运单号进行打印" />
			</view>
			<tn-button @tap="searchNum()" backgroundColor="tn-bg-blue" fontColor="#fff">确 定</tn-button>
		</view>
		<view class="main">
			<view class="content">
				<view class="name">
					主运单号:
				</view>
				<view class="search" style="background-color: #f5f5f5;">
					<input v-model="orderInfo.first_waybill_no" disabled style="font-size: 28rpx;" class="uni-input"
						confirm-type="search" placeholder="主运单号" />
				</view>
			</view>
			<view class="content">
				<view class="name">
					从运单号:
				</view>
				<view class="search" style="background-color: #f5f5f5;">
					<input v-model="orderInfo.second_waybill_no" disabled style="font-size: 28rpx;" class="uni-input"
						confirm-type="search" placeholder="从运单号" />
				</view>
			</view>
			<view class="content">
				<view class="name">
					用户昵称:
				</view>
				<view class="search" style="background-color: #f5f5f5;">
					<input v-model="orderInfo.user.username" disabled style="font-size: 28rpx;" class="uni-input"
						confirm-type="search" placeholder="用户昵称" />
				</view>
			</view>
			<view class="content">
				<view class="name">
					客户唛头:
				</view>
				<view class="search" style="background-color: #f5f5f5;">
					<input v-model="orderInfo.receiver.user_mark" disabled style="font-size: 28rpx;" class="uni-input"
						confirm-type="search" placeholder="客户唛头" />
				</view>
			</view>
			<view class="content" style="align-items:flex-start;">
				<view class="name">
					订单备注:
				</view>
				<view class="search" style="background-color: #f5f5f5;width: 75%;">
					<textarea v-model="orderInfo.remarks" disabled style="font-size: 28rpx;height: 100rpx"
						class="uni-input" confirm-type="search" placeholder="订单备注" />
				</view>
			</view>
			<view class="content" v-if="orderInfo.first_waybill_no">
				主运单打印次数: <text style="color: #3D7EFF;margin-right: 20rpx;">{{orderInfo.print_first_count}}</text>
				从运单打印次数: <text style="color: #3D7EFF;">{{orderInfo.print_second_count}}</text>
			</view>
		</view>
		<view class="msg" style="color: red;" v-if="errMsg">
			重复打印提醒:该运单号重复打印
		</view>
		<view class="" style="margin-top: 20rpx;">
			<tn-radio-group v-model="printType" @change="radioGroupChange">
				<tn-radio activeColor="#3D7EFF" v-for="(item, index) in printTypelist" :key="index" :name="item.name"
					:disabled="item.disabled">
					{{item.name}}
				</tn-radio>
			</tn-radio-group>
		</view>
		<view class="" style="display: flex;justify-content: center;margin-top: 50rpx;">
			<tn-button @tap="print()" width="550rpx" height="80rpx" backgroundColor="tn-bg-blue"
				fontColor="tn-color-white">打印面单</tn-button>
		</view>
		<canvas style="opacity: 0;" canvas-id="myCanvas" id="myCanvas" />
		<view v-show="showBluetooth" class="cu-dialog" @tap="showBluetooth = false">
			<view class="dialog-content">
				<view class="cu-bar" style="margin-bottom: 20rpx;">
					<view class="content" style="color: #ecd2ac;">蓝牙设备</view>
				</view>
				<scroll-view class="v30" scroll-y="true">
					<view class="cu-list">
						<view @tap.stop="connect(item)" class="cu-item" v-for="(item,index) in bluetoothList"
							:key="index">
							<view v-if="item.name">{{item.name}} <text
									style="color: black;font-weight: 700;margin-left: 20rpx;">{{item.address}}</text>
							</view>
							<view v-if="item.name ==' '">
								无名称
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		overwriteImageData
	} from "@/common/getPrintData.js"
	import scan from "@/common/pdaScan.vue"
	export default {
		components: {
			scan
		},
		data() {
			return {
				errMsg: false,
				bluetoothSocket: null,
				device: null,
				canvasInput: null,
				interval: null,
				waybill_no: "",
				orderInfo: {
					receiver: {},
					sender: {},
					user: {},
					waybill_data: {},
					item: [{
						title: "",
						num: 1
					}]
				},
				bluetoothList: [],
				showBluetooth: false,
				bluetoothStyle: "未连接",
				printTypelist: [{
					name: '打印扫描运单号',
					disabled: false
				}, {
					name: '打印末端单号',
					disabled: false
				}],
				printType: '打印扫描运单号'
			}
		},
		onLoad() {
			this.getDate()
			if (uni.getStorageSync("bluetoothPrint")) {
				this.connect(JSON.parse(uni.getStorageSync("bluetoothPrint")))
			}
			// setTimeout(() => {
			// 	const context = uni.createCanvasContext('myCanvas')
			// 	uni.downloadFile({
			// 		url: "http://cdn-cloudprint.cainiao.com/waybill-print/cloudprint-imgs/731999ce60f6450290a5f05703d49f64.jpg",
			// 		success: function(res) {
			// 			context.drawImage(res.tempFilePath, 0, 0, 184, 56)
			// 			context.draw()
			// 			resove("绘图成功")
			// 		}
			// 	})
			// }, 500)

		},
		onShow: function() {
			var that = this
			uni.$off('scancodedate') // 每次进来先 移除全局自定义事件监听器  
			uni.$on('scancodedate', function(data) {
				that.waybill_no = data.code
				that.searchNum()
			})
		},
		onUnload() {
			this.closeBluetooth(); //必须关闭蓝牙连接否则意外断开的话打印错误  
		},
		methods: {
			radioGroupChange(e) {
				console.log(this.printType);
			},
			closeBluetooth() {
				if (!this.bluetoothSocket) return true
				this.bluetoothSocket.close();
				this.bluetoothSocket = null
			},
			closeBlue() {
				uni.setStorageSync("bluetoothPrint", "")
				this.bluetoothStyle = "未连接"
				if (!this.bluetoothSocket) return true
				this.bluetoothSocket.close();
				this.bluetoothSocket = null
			},
			//1.  页面初始化，获取位置，打开蓝牙连接（必须打开定位）
			getBluetooth() {
				this.showBluetooth = true
				let system = uni.getSystemInfoSync(); // 获取系统信息
				if (system.platform === 'android') { // 判断平台
					var that = this
					var context = plus.android.importClass("android.content.Context");
					var locationManager = plus.android.importClass("android.location.LocationManager");
					var main = plus.android.runtimeMainActivity();
					var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
					// 定位检测
					if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
						uni.showModal({
							title: '提示',
							content: '请打开定位服务功能',
							showCancel: false, // 不显示取消按钮
							success() {
								if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
									var Intent = plus.android.importClass('android.content.Intent');
									var Settings = plus.android.importClass('android.provider.Settings');
									var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
									main.startActivity(intent); // 打开系统设置GPS服务页面
								} else {
									console.log('GPS功能已开启');
								}
							}
						});
						return false
					} else {
						console.log('GPS功能已开启');
						that.searchDevices()
						return true
					}
				}
			},
			//2. 搜索蓝牙
			searchDevices() {

				var deviceList = [];
				var main = plus.android.runtimeMainActivity();
				var Context = plus.android.importClass("android.content.Context");
				var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
				plus.android.importClass(BManager);
				var BAdapter = BManager.getAdapter();
				plus.android.importClass(BAdapter);
				var lists = BAdapter.getBondedDevices();
				plus.android.importClass(lists);
				var iterator = lists.iterator();
				plus.android.importClass(iterator);
				while (iterator.hasNext()) {
					var d = iterator.next();
					plus.android.importClass(d);
					var temp = {
						name: d.getName(),
						address: d.getAddress(),
						status: d.getBondState(),
						uuids: d.getUuids(),
						op: d
					};
					deviceList.push(temp);
				}
				console.log(deviceList)
				this.bluetoothList = deviceList
				// return deviceList;

			},
			scanCode() {
				var that = this
				// 允许从相机和相册扫码
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.waybill_no = res.result
						that.searchNum()
					}
				});
			},
			searchNum() {
				this.errMsg = false
				uni.showLoading({
					title: '获取扫描信息中...',
					icon: 'none'
				});
				this.$api.admin({
					apiUrl: '/print/expressLabel',
					param: {
						waybill_no: this.waybill_no
					}
				}).then((res) => {
					this.orderInfo = res.data.data
					this.getPrintNum()
					this.getCanvas()
					uni.hideLoading();
				}).catch((err) => {
					uni.hideLoading();
				})
			},
			//重复打印检测
			getPrintNum() {
				this.$api.admin({
					apiUrl: '/print/checkExpress',
					param: {
						waybill_nos: [this.waybill_no]
					}
				}).then((res) => {
					if (res.data.data.errorData.length) {
						this.errMsg = true
					}
				}).catch((err) => {})
			},
			//链接打印机
			connect: function(item) {
				if (this.bluetoothSocket) {
					this.bluetoothSocket.close();
				}
				this.main = plus.android.runtimeMainActivity();
				var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
				var UUID = plus.android.importClass("java.util.UUID");
				var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
				var BAdapter = BluetoothAdapter.getDefaultAdapter();
				this.device = BAdapter.getRemoteDevice(item.address);
				plus.android.importClass(this.device);
				this.bluetoothSocket = this.device.createInsecureRfcommSocketToServiceRecord(uuid);
				plus.android.importClass(this.bluetoothSocket);
				if (!this.bluetoothSocket.isConnected()) {
					uni.showLoading({
						title: '检测到设备未连接，尝试连接....'
					})
					this.bluetoothSocket.connect();
				}
				uni.hideLoading()
				uni.setStorageSync("bluetoothPrint", JSON.stringify(item))
				this.bluetoothStyle = item.name
				this.showBluetooth = false
				console.log('设备已连接')
			},
			getDate() {
				var newDate = new Date();
				var month = newDate.getMonth() + 1
				var date = newDate.getDate()
				if (month < 10) {
					month = '0' + month
				}
				if (date < 10) {
					date = '0' + date
				}
				console.log(newDate.getFullYear() + "/" + month + "/" + date)
				return newDate.getFullYear() + "/" + month + "/" + date
			},
			getTime() {
				var myDate = new Date();
				var hours = myDate.getHours()
				var minutes = myDate.getMinutes()
				var seconds = myDate.getSeconds()
				if (hours < 10) {
					hours = '0' + hours
				}
				if (minutes < 10) {
					minutes = '0' + minutes
				}
				if (seconds < 10) {
					seconds = '0' + seconds
				}
				return hours + ":" + minutes + ":" + seconds
			},
			getCanvas() {
				var that = this
				return new Promise((resove, reject) => {
					const context = uni.createCanvasContext('myCanvas')
					uni.downloadFile({
						url: that.orderInfo.port_img,
						success: function(res) {
							context.drawImage(res.tempFilePath, 0, 0, 184, 56)
							context.draw()
							resove("绘图成功")
						}
					})
				})

			},
			async print() {
				if (!this.bluetoothSocket) {
					this.$t.message.toast('请先连接打印机')
					return
				}
				var that = this
				if (!that.orderInfo.waybill_no) {
					that.$t.message.toast('暂无订单信息')
					return
				}
				// var outputStream = that.bluetoothSocket.getOutputStream();
				// plus.android.importClass(outputStream);
				// var goodsData = ''
				// var height = 690
				// that.orderInfo.item.forEach((i, n) => {
				// 	if (n == 0) {
				// 		goodsData += "TEXT 8 0 10 690 商品信息:" + "" + i.title + "*" + i.num +
				// 			"\r\n"
				// 	} else if (n > 0 && n < 5) {
				// 		goodsData += "TEXT 8 0 120 " + (Number(height) + n * 30) + " " + i
				// 			.title + "*" + i
				// 			.num +
				// 			"\r\n"
				// 	} else if (n == 5) {
				// 		goodsData += "TEXT 8 0 120 " + (Number(height) + n * 30) + " ...共" +
				// 			that
				// 			.orderInfo.item.length + "种商品\r\n"
				// 	}
				// })
				// let position = ''
				// let position_no = ''
				// let package_wdjc = ''
				// if (that.orderInfo.waybill_data.position == undefined) {
				// 	position = '暂无'
				// } else {
				// 	position = that.orderInfo.waybill_data.position
				// }
				// if (that.orderInfo.waybill_data.position_no == undefined) {
				// 	position_no = '暂无'
				// } else {
				// 	position_no = that.orderInfo.waybill_data.position_no
				// }
				// if (that.orderInfo.waybill_data.package_wdjc == undefined) {
				// 	package_wdjc = '暂无'
				// } else {
				// 	package_wdjc = that.orderInfo.waybill_data.package_wdjc
				// }
				// //打印命令
				// var path =
				// 	"! 0 200 200 1050 1\r\n" +
				// 	// "EG 23 56 0 10\r\n" +
				// 	// a.join('') + "\r\n" +
				// 	// "LINE 1 90 580 90 2\r\n" +
				// 	// "CENTER\r\n" +
				// 	"SETMAG 3 3\r\n" +
				// 	"TEXT 8 0 10 0 " + position + "-" +
				// 	position_no + "\r\n" +
				// 	"SETMAG 1 1\r\n" +

				// 	"LINE 1 90 580 90 2\r\n" +

				// 	// ---集字方框
				// 	"BOX 10 110 70 175 5\r\n" +
				// 	"SETMAG 2 2\r\n" +
				// 	"TEXT 8 0 17 120 集\r\n" +

				// 	"SETMAG 3 3\r\n" +
				// 	"TEXT 8 0 90 105" + " " + package_wdjc + "\r\n" +
				// 	"SETMAG 1 1\r\n" +
				// 	"LINE 1 180 580 180 2\r\n" +

				// 	// 收字图标
				// 	"EG 9 79 10 215 00000003FFC00000000000007FFFFC000000000003FFFFFF80000000000FFFFFFFE0000000003FFFFFFFF800000000FFFFFFFFFE00000001FFFFFFFFFF80000007FFFFFFFFFFC000000FFFF8003FFFE000001FFFC00007FFF000003FFE000000FFF800007FFC0000003FFC0000FFF00000001FFE0001FFE0380E000FFF0001FFC0381E0003FF0003FF80381C0001FF8007FF00381C0000FFC007FE1838380000FFC007FC1C383800007FE00FF81C387FFFF83FE00FF81C387FFFF83FE01FF01C38FFFFF01FF01FF01C38F007001FF01FF01C39F007000FF01FE01C39F007000FF83FE01C3BF007000FF83FE01C3BB807000FF83FE01C3B380F000FF83FE01C38380E000FF83FE01C381C0E0007F83FE01C381C1E000FF83FE01C381E1C000FF83FE01C780E3C000FF81FE01FF80F38000FF81FE01FF807F8000FF01FF03F3803F0000FF01FF03C3801E0001FF00FF0183803F0001FE00FF8003807FC003FE007F800381F3F003FC007FC00387E1FC07FC003FE0039F807F87F8003FE003BF001F8FF8001FF003BC00071FF0001FF8011000003FF0000FFC000000007FE00007FF00000000FFE00007FF80000003FFC00003FFE0000007FF800001FFF800003FFF800001FFFF0000FFFF000000FFFFF01FFFFE0000007FFFFFFFFFFE0000007FFFFFFFFFFC0000003FFFFFFFFFF80000001FFFFFFFFFF00000000FFFFFFFFFF00000000FFFFFFFFFE000000007FFFFFFFFC000000003FFFFFFFF8000000001FFFFFFFF0000000000FFFFFFFF0000000000FFFFFFFE00000000007FFFFFFC00000000003FFFFFF800000000001FFFFFF000000000000FFFFFE0000000000007FFFFC0000000000003FFFFC0000000000003FFFF80000000000001FFFF00000000000000FFFE000000000000007FFC000000000000003FF8000000000000001FF0000000000000000FE00000000000000007C00000000000000003800000000000000001000000000\r\n" +

				// 	"TEXT 8 0 80 205" + " " + that.orderInfo.receiver.name + "   +" + that.orderInfo
				// 	.receiver.area_code + " " + that.orderInfo.receiver.mobile + "\r\n" +
				// 	"SETMAG 1 1\r\n" +
				// 	"TEXT 8 0 80 235" + " " + that.orderInfo.receiver.nation + that.orderInfo.receiver
				// 	.province + that.orderInfo.receiver.city + that.orderInfo.receiver.district +
				// 	"\r\n" +
				// 	"TEXT 8 0 80 265" + " " + that.orderInfo.receiver.detailed + "\r\n" +

				// 	"LINE 1 315 580 315 2\r\n" +

				// 	// 寄件信息
				// 	"SETMAG 2 2\r\n" +
				// 	"TEXT 8 0 20 325 寄\r\n" +
				// 	"SETMAG 1 1\r\n" +
				// 	"TEXT 8 0 80 325" + " " + that.orderInfo.sender.name + "   +" + that.orderInfo
				// 	.sender.area_code + " " + that.orderInfo.sender.mobile + "\r\n" +
				// 	"TEXT 8 0 80 355" + " " + that.orderInfo.sender.nation + that.orderInfo.sender
				// 	.province + that.orderInfo.sender.city + that.orderInfo.sender.district + that
				// 	.orderInfo.sender.detailed + "\r\n" +
				// 	"LINE 1 395 580 395 2\r\n" +
				// 	//一维码
				// 	"BARCODE-TEXT 7 0 5\r\n" +
				// 	"BARCODE 128 2 1 80 100 405" + " " + that.orderInfo.waybill_no + "\r\n" +
				// 	"BARCODE-TEXT OFF\r\n" +
				// 	"LINE 1 525 580 525 2\r\n" +
				// 	//日期
				// 	"SETMAG 1 1\r\n" +
				// 	"TEXT 8 0 10 555 " + that.getDate() + "\r\n" +
				// 	"TEXT 8 0 10 585 " + that.getTime() + "\r\n" +
				// 	"TEXT 8 0 10 615 1/1\r\n" +
				// 	"LINE 140 526 140 685 2\r\n" +
				// 	//说明
				// 	"SETMAG 0 0\r\n" +
				// 	"TEXT 3 0 150 555 签字代表包裹已送达，且\r\n" +
				// 	"TEXT 3 0 150 585 包裹商品信息无误、包装\r\n" +
				// 	"TEXT 3 0 150 615 完好、无破损等质量问题\r\n" +
				// 	// "TEXT 3 0 150 620 \r\n" +
				// 	// "TEXT 3 0 150 635 没有划痕、破损等表面质量问题。\r\n" +
				// 	"LINE 430 526 430 685 2\r\n" +
				// 	//签字栏
				// 	"SETMAG 1 1\r\n" +
				// 	"TEXT 8 0 435 650 签字栏\r\n" +
				// 	"LINE 1 686 580 686 2\r\n" +
				// 	//商品信息
				// 	// "TEXT 8 0 10 790 商品信息:" + "" + that.orderInfo.item[0].title + "*" + that.orderInfo
				// 	// .item[0].num + "\r\n" +
				// 	goodsData +
				// 	//备注  价值
				// 	"TEXT 8 0 10 860 备注:" + "" + that.orderInfo.remarks + "\r\n" +
				// 	// "TEXT 8 0 420 900 物品价值: $999.99\r\n" +
				// 	"LINE 1 890 580 890 2\r\n" +
				// 	"TEXT 8 0 10 900 订单号:" + "" + that.orderInfo.waybill_no + "\r\n" +
				// 	"TEXT 8 0 480 900 已检视\r\n" +
				// 	"PRINT\r\n";
				// var bytes = plus.android.invoke(path, 'getBytes', 'gbk');

				// outputStream.write(bytes);

				// outputStream.flush();

				// that.device = null //这里关键
				// that.backPrint()

				let printStatus = await this.getCanvas()
				console.log(printStatus)
				uni.canvasGetImageData({
					canvasId: 'myCanvas',
					x: 0,
					y: 0,
					width: 184,
					height: 56,
					success(res) {
						let data = overwriteImageData(res)
						console.log(data)
						var outputStream = that.bluetoothSocket.getOutputStream();
						plus.android.importClass(outputStream);
						let a = []
						data.array.forEach(item => {
							a.push(('00' + item.toString(16)).slice(-2).toUpperCase())
						})
						var goodsData = ''
						var height = 790
						that.orderInfo.item.forEach((i, n) => {
							if (n == 0) {
								goodsData += "TEXT 8 0 10 790 商品信息:" + "" + i.title + "*" + i.num +
									"\r\n"
							} else if (n > 0 && n < 3) {
								goodsData += "TEXT 8 0 120 " + (Number(height) + n * 30) + " " + i
									.title + "*" + i
									.num +
									"\r\n"
							} else if (n == 3) {
								goodsData += "TEXT 8 0 120 " + (Number(height) + n * 30) + " ...共" +
									that
									.orderInfo.item.length + "种商品\r\n"
							}
						})
						console.log(goodsData)
						//打印命令
						var path =
							"! 0 200 200 1050 1\r\n" +
							"EG 23 56 0 10\r\n" +
							a.join('') + "\r\n" +
							"LINE 1 90 580 90 2\r\n" +
							// "CENTER\r\n" +
							"SETMAG 3 3\r\n" +
							"TEXT 8 0 10 100 " + that.orderInfo.waybill_data.position + "-" +
							that.orderInfo.waybill_data.position_no + "\r\n" +
							"SETMAG 1 1\r\n" +

							"LINE 1 200 580 200 2\r\n" +

							// ---集字方框
							"BOX 10 210 70 275 5\r\n" +
							"SETMAG 2 2\r\n" +
							"TEXT 8 0 17 220 集\r\n" +

							"SETMAG 3 3\r\n" +
							"TEXT 8 0 90 205" + " " + that.orderInfo.waybill_data.package_wdjc + "\r\n" +
							"SETMAG 1 1\r\n" +
							"LINE 1 280 580 280 2\r\n" +

							// 收字图标
							"EG 9 79 10 315 00000003FFC00000000000007FFFFC000000000003FFFFFF80000000000FFFFFFFE0000000003FFFFFFFF800000000FFFFFFFFFE00000001FFFFFFFFFF80000007FFFFFFFFFFC000000FFFF8003FFFE000001FFFC00007FFF000003FFE000000FFF800007FFC0000003FFC0000FFF00000001FFE0001FFE0380E000FFF0001FFC0381E0003FF0003FF80381C0001FF8007FF00381C0000FFC007FE1838380000FFC007FC1C383800007FE00FF81C387FFFF83FE00FF81C387FFFF83FE01FF01C38FFFFF01FF01FF01C38F007001FF01FF01C39F007000FF01FE01C39F007000FF83FE01C3BF007000FF83FE01C3BB807000FF83FE01C3B380F000FF83FE01C38380E000FF83FE01C381C0E0007F83FE01C381C1E000FF83FE01C381E1C000FF83FE01C780E3C000FF81FE01FF80F38000FF81FE01FF807F8000FF01FF03F3803F0000FF01FF03C3801E0001FF00FF0183803F0001FE00FF8003807FC003FE007F800381F3F003FC007FC00387E1FC07FC003FE0039F807F87F8003FE003BF001F8FF8001FF003BC00071FF0001FF8011000003FF0000FFC000000007FE00007FF00000000FFE00007FF80000003FFC00003FFE0000007FF800001FFF800003FFF800001FFFF0000FFFF000000FFFFF01FFFFE0000007FFFFFFFFFFE0000007FFFFFFFFFFC0000003FFFFFFFFFF80000001FFFFFFFFFF00000000FFFFFFFFFF00000000FFFFFFFFFE000000007FFFFFFFFC000000003FFFFFFFF8000000001FFFFFFFF0000000000FFFFFFFF0000000000FFFFFFFE00000000007FFFFFFC00000000003FFFFFF800000000001FFFFFF000000000000FFFFFE0000000000007FFFFC0000000000003FFFFC0000000000003FFFF80000000000001FFFF00000000000000FFFE000000000000007FFC000000000000003FF8000000000000001FF0000000000000000FE00000000000000007C00000000000000003800000000000000001000000000\r\n" +

							"TEXT 8 0 80 305" + " " + that.orderInfo.receiver.name + "   +" + that.orderInfo
							.receiver.area_code + " " + that.orderInfo.receiver.mobile + "\r\n" +
							"SETMAG 1 1\r\n" +
							"TEXT 8 0 80 335" + " " + that.orderInfo.receiver.nation + that.orderInfo.receiver
							.province + that.orderInfo.receiver.city + that.orderInfo.receiver.district +
							"\r\n" +
							"TEXT 8 0 80 365" + " " + that.orderInfo.receiver.detailed + "\r\n" +

							"LINE 1 415 580 415 2\r\n" +

							// 寄件信息
							"SETMAG 2 2\r\n" +
							"TEXT 8 0 20 425 寄\r\n" +
							"SETMAG 1 1\r\n" +
							"TEXT 8 0 80 425" + " " + that.orderInfo.sender.name + "   +" + that.orderInfo
							.sender.area_code + " " + that.orderInfo.sender.mobile + "\r\n" +
							"TEXT 8 0 80 455" + " " + that.orderInfo.sender.nation + that.orderInfo.sender
							.province + that.orderInfo.sender.city + that.orderInfo.sender.district + that
							.orderInfo.sender.detailed + "\r\n" +
							"LINE 1 495 580 495 2\r\n" +
							//一维码
							"BARCODE-TEXT 7 0 5\r\n" +
							"BARCODE 128 2 1 80 100 505" + " " + that.orderInfo.waybill_no + "\r\n" +
							"BARCODE-TEXT OFF\r\n" +
							"LINE 1 625 580 625 2\r\n" +
							//日期
							"SETMAG 1 1\r\n" +
							"TEXT 8 0 10 655 " + that.getDate() + "\r\n" +
							"TEXT 8 0 10 685 " + that.getTime() + "\r\n" +
							"TEXT 8 0 10 715 1/1\r\n" +
							"LINE 140 626 140 785 2\r\n" +
							//说明
							"TEXT 3 0 150 655 快件送达收件人地址，经收件人或收件\r\n" +
							"TEXT 3 0 150 675 人（寄件人）允许的代收人签字，视为\r\n" +
							"TEXT 3 0 150 695 送达。您的签字代表您已经签收此包裹\r\n" +
							"TEXT 3 0 150 715 ，并已确认商品信息无误、包装完好、\r\n" +
							"TEXT 3 0 150 735 没有划痕、破损等表面质量问题。\r\n" +
							"LINE 430 626 430 785 2\r\n" +
							//签字栏
							"SETMAG 1 1\r\n" +
							"TEXT 8 0 435 750 签字栏\r\n" +
							"LINE 1 786 580 786 2\r\n" +
							//商品信息
							// "TEXT 8 0 10 790 商品信息:" + "" + that.orderInfo.item[0].title + "*" + that.orderInfo
							// .item[0].num + "\r\n" +
							goodsData +
							//备注  价值
							"TEXT 8 0 10 920 备注:" + "" + that.orderInfo.remarks + "\r\n" +
							// "TEXT 8 0 420 900 物品价值: $999.99\r\n" +
							"LINE 1 950 580 950 2\r\n" +
							"TEXT 8 0 10 970 订单号:" + "" + that.orderInfo.waybill_no + "\r\n" +
							"TEXT 8 0 480 970 已检视\r\n" +
							"PRINT\r\n";
						var bytes = plus.android.invoke(path, 'getBytes', 'gbk');

						outputStream.write(bytes);

						outputStream.flush();

						that.device = null //这里关键



					}
				})

			},
			//打印回调
			backPrint() {
				var that = this
				console.log(that.orderInfo.tid + '|' + that.waybill_no);
				this.$api.admin({
					apiUrl: '/order/printTimes',
					param: {
						"document_id": that.orderInfo.tid + '|' + that.waybill_no,
						"type": "order"
					}
				}).then((res) => {
					console.log(res);
				}).catch((err) => {})
			}

		}
	}
</script>

<style scoped lang="scss">
	.search-view {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.search {
		border: 1rpx solid #d9d9d9;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
		width: 80%;
		display: flex;
		align-items: center;
	}

	.main {
		border: 1rpx solid #d9d9d9;
		border-radius: 10rpx;
		padding: 40rpx;
		margin-top: 40rpx;
		font-size: 30rpx;

		.content {
			display: flex;
			align-items: center;
			margin-bottom: 30rpx;

			.name {
				margin-right: 10rpx;
				width: 25%;
				text-align: right;
			}
		}
	}

	.bluetooth {
		font-size: 28rpx;
		margin-bottom: 20rpx;
		background-color: #3D7EFF;
		text-align: center;
		color: #fff;
		padding: 10rpx 20rpx;
		display: inline-block;
		border-radius: 10rpx;
	}

	.cu-dialog {
		padding: 40rpx 0;
	}

	.cu-bar {
		font-size: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		.content {
			margin-right: 10rpx;
			position: relative;
			top: -3rpx;
		}
	}

	.v30 {
		height: 50vh;

		.cu-list {
			.cu-item {
				text-align: center;
				padding: 30rpx 0;
				border-bottom: 1rpx solid #ecd2ac;
				color: #666;
			}
		}

	}

	.cu-dialog {
		position: fixed;
		background-color: rgba(0, 0, 0, .3);
		width: 100%;
		height: 100vh;
		top: 0;
		left: 0;

		.dialog-content {
			background-color: #fff;
			position: absolute;
			bottom: 0;
			width: 100%;
			padding: 20rpx;
		}
	}
</style>